﻿using demo.Interfaces;
using demo.Models.DTOs;
using demo.Models;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;
using System.Collections.Generic;
using demo.Services;

namespace demo.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class StockController : ControllerBase
    {
        private readonly IStockService _stockService;

        public StockController(IStockService stockService)
        {
            _stockService = stockService;
        }

        /// <summary>
        /// 添加入库订单
        /// </summary>
        /// <param name="stockInDto">入库订单数据</param>
        /// <returns>操作结果</returns>
        [HttpPost("add-stock-in")]
        public async Task<IActionResult> AddStockIn([FromBody] StockInDto stockInDto)
        {
            try
            {
                await _stockService.AddStockInAsync(stockInDto);
                return Ok(new { message = "入库订单添加成功" });
            }
            catch (ArgumentException ex)
            {
                return BadRequest(new { message = ex.Message });
            }
            catch (Exception ex)
            {
                return StatusCode(500, new { message = "添加入库订单时发生错误", details = ex.Message });
            }
        }

        /// <summary>
        /// 添加出库订单
        /// </summary>
        /// <param name="stockOutDto">出库订单数据</param>
        /// <returns>操作结果</returns>
        [HttpPost("add-stock-out")]
        public async Task<IActionResult> AddStockOut([FromBody] StockOutDto stockOutDto)
        {
            try
            {
                await _stockService.AddStockOutAsync(stockOutDto);
                return Ok(new { message = "出库订单添加成功" });
            }
            catch (ArgumentException ex)
            {
                return BadRequest(new { message = ex.Message });
            }
            catch (Exception ex)
            {
                return StatusCode(500, new { message = "添加出库订单时发生错误", details = ex.Message });
            }
        }

        /// <summary>
        /// 获取入库明细（分页）
        /// </summary>
        /// <param name="page">当前页码</param>
        /// <param name="pageSize">每页大小</param>
        /// <returns>入库明细列表</returns>
        [HttpGet("stock-in-details")]
        public async Task<IActionResult> GetStockInDetails([FromQuery] int page, [FromQuery] int pageSize)
        {
            //新增一个统计总数的按钮

            try
            {
                var stockInDetails = await _stockService.GetStockInDetailsAsync(page, pageSize);
                var totalRecords = await _stockService.GetAllStockinsCountAsync();
                var result = new
                {
                    items  = stockInDetails,
                    totalRecords = totalRecords 
                };
                return Ok(result);
            }
            catch (ArgumentException ex)
            {
                return BadRequest(new { message = ex.Message });
            }
            catch (Exception ex)
            {
                return StatusCode(500, new { message = "获取入库明细时发生错误", details = ex.Message });
            }
        }

        [HttpGet("stock-out-details")]
        public async Task<IActionResult> GetStockOutDetailsAsync([FromQuery] int page, [FromQuery] int pageSize)
        {
            if (page <= 0 || pageSize <= 0)
            {
                return BadRequest("Page and pageSize must be greater than 0.");
            }

            var stockOutDetails = await _stockService.GetStockOutDetailsAsync(page, pageSize);
            var totalRecords = await _stockService.GetAllStockoutsCountAsync();
            var result = new
            {
                items = stockOutDetails,
                totalRecords = totalRecords
            };
            return Ok(result);
        }

        [HttpGet("stock-records")]
        public async Task<IActionResult> GetStockRecordsAsync([FromQuery] int productId, [FromQuery] DateTime startDate, [FromQuery] DateTime endDate)
            
        {
        if (startDate > endDate)
        {
            return BadRequest("StartDate cannot be later than EndDate.");
        }

        var stockRecords = await _stockService.GetStockRecordsAsync(productId, startDate, endDate);
        return Ok(stockRecords);
        }

    /// <summary>
    /// 获取指定日期的入库和出库总额
    /// </summary>
        [HttpGet("daily-summary")]
        public async Task<IActionResult> GetDailyStockSummaryAsync([FromQuery] DateTime date)
        {
            var dailySummary = await _stockService.GetDailyStockSummaryAsync(date);
            return Ok(dailySummary);
        }
    }
}
